import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_image_gallery/lf_image_gallery_logic.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_fonts.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/utils/lf_scroll_behavior.dart';
import 'package:largefriends/widgets/custom_cache_image.dart';
import 'package:largefriends/widgets/preload_page_view.dart';

class LfImageGalleryPage extends StatefulWidget {
  const LfImageGalleryPage({super.key});

  @override
  State<LfImageGalleryPage> createState() => _LfImageGalleryPageState();
}

class _LfImageGalleryPageState extends State<LfImageGalleryPage> {
  final logic = Get.put(LfImageGalleryLogic());
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.black26,
            leading: InkWell(
              onTap: () {
                Get.back();
              },
              child: Icon(
                Icons.arrow_back,
                color: colorFEFEFE,
              ),
            ),
            automaticallyImplyLeading: false,
            centerTitle: false,
            title: Obx(() => Text(
                  "${logic.currentIndex.value + 1}/${logic.urls.length}",
                  style: lfTextStyle(
                    sp: 16.sp,
                    fontFamily: fontBold,
                    color: colorFEFEFE,
                  ),
                )),
          ),
          extendBodyBehindAppBar: true,
          body: ScrollConfiguration(
            behavior: LfScrollBehavior(),
            child: PreloadPageView.builder(
              itemBuilder: (context, index) {
                return InkWell(
                  onTap: () {
                    Get.back();
                  },
                  child: Container(
                    width: ScreenUtil().screenWidth,
                    height: ScreenUtil().screenHeight,
                    alignment: Alignment.center,
                    color: Colors.black,
                    child: InkWell(
                      child: CustomCacheImage(
                        imageUrl: logic.urls[index],
                        extendedImageMode: ExtendedImageMode.gesture,
                      ),
                    ),
                  ),
                );
              },
              itemCount: logic.urls.length,
              controller: logic.controller,
              preloadPagesCount: logic.urls.length,
              onPageChanged: (value) {
                logic.currentIndex.value = value;
              },
            ),
          ),
        ),
        onWillPop: () async {
          Get.back();
          return false;
        });
  }
}
